Imports EnvDTE
Imports EnvDTE80

Class FilePowerTool
    Inherits BaseTool

    Public Sub New()
    End Sub

    Public Overrides Function AvailableItems(ByVal project As EnvDTE.Project) As System.Collections.Generic.List(Of String)
        Dim fileService As New Data.FileService.fileService
        fileService.Url = GetServiceURL(project)

        Dim folder As String = ProjectOptionsManager.UmbracoFolder(project)
        Dim username As String = ProjectOptionsManager.UmbracoUser(project)
        Dim password As String = ProjectOptionsManager.UmbracoPassword(project)
        
        Dim loadeditems As String() = fileService.GetFilesList(folder, username, password)

        Dim items As New Generic.List(Of String)
        items.AddRange(loadeditems)

        Return items
    End Function

    Public Overrides ReadOnly Property ToolName() As String
        Get
            Return "File Tool"
        End Get
    End Property

    Public Function GetServiceURL(ByVal project As Project) As String
        Return ProjectOptionsManager.UmbracoSite(project) & "/webservices/FileService.asmx"
    End Function

    Public Overrides Sub DownloadItem(ByVal projectItem As EnvDTE.ProjectItem)
        Try
            Dim project As Project = projectItem.ContainingProject
            Dim fileService As New Data.FileService.fileService
            fileService.Url = GetServiceURL(projectItem.ContainingProject)

            Dim folder As String = ProjectOptionsManager.UmbracoFolder(project)
            Dim username As String = ProjectOptionsManager.UmbracoUser(project)
            Dim password As String = ProjectOptionsManager.UmbracoPassword(project)
            Dim fileName As String = projectItem.Name
            Dim fileLocation As String = projectItem.FileNames(1)
            Dim data() As Byte = Nothing

            data = fileService.DownloadFile(folder, fileName, username, password)
            Dim fileStream As New IO.FileStream(fileLocation, IO.FileMode.Create, IO.FileAccess.ReadWrite)
            fileStream.Write(data, 0, data.Length)
            fileStream.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Public Overrides Sub UploadItem(ByVal projectItem As EnvDTE.ProjectItem)
        Try

            If projectItem.IsOpen Then
                projectItem.Save()
            End If

            Dim project As Project = projectItem.ContainingProject
            Dim fileService As New Data.FileService.fileService
            fileService.Url = GetServiceURL(projectItem.ContainingProject)

            Dim folder As String = ProjectOptionsManager.UmbracoFolder(project)
            Dim username As String = ProjectOptionsManager.UmbracoUser(project)
            Dim password As String = ProjectOptionsManager.UmbracoPassword(project)
            Dim fileName As String = projectItem.Name
            Dim fileLocation As String = projectItem.FileNames(1)


            Dim file As System.IO.FileInfo = New System.IO.FileInfo(fileLocation)
            Dim fileStream As System.IO.FileStream = file.OpenRead()
            Dim bytesSize As Integer = CInt(fileStream.Length)
            Dim fileData(bytesSize - 1) As Byte
            fileStream.Read(fileData, 0, bytesSize)
            fileStream.Close()
            ' MsgBox(fileData.ToString)
            fileService.UploadFile(fileData, folder, fileName, username, password, True)

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Public Overrides ReadOnly Property ToolDescription() As String
        Get
            Return "Tool to edit files"
        End Get
    End Property


End Class